﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;
using System.IO;

public partial class CertificateDataEN : System.Web.UI.Page
{

    public bool isImagesOk = true;

    public CertificateData mobjCertificateData
    {
        get
        {
            if (ViewState["mobjCertificateData"] == null)
                ViewState["mobjCertificateData"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjCertificateData"].ToString()] as CertificateData;
        }
        set
        {
            if (ViewState["mobjCertificateData"] == null)
                ViewState["mobjCertificateData"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjCertificateData"].ToString()] = value;
        }
    }


    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            checkSecurityAndCustomizePage();

            BindAllControls();

        }



    }


    /// <summary>
    /// this function will bind all controls with values found in object mobjCertificateData
    /// </summary>
    private void BindAllControls()
    {
        txtManagerName.Text = mobjCertificateData.ManagerName;
        txtManagerPosition.Text = mobjCertificateData.ManagerPos;


        imgIamLearning.Src = "getImage.ashx?image=./CertificateImages/" + mobjCertificateData.IamLearningImage + "&w=100&h=100&color=white";
        imgSecLogo.Src = "getImage.ashx?image=./CertificateImages/" + mobjCertificateData.SECImage + "&w=100&h=100&color=white";
        imgSign.Src = "getImage.ashx?image=./CertificateImages/" + mobjCertificateData.SignImage + "&w=100&h=100&color=white";
        imgStamp.Src = "getImage.ashx?image=./CertificateImages/" + mobjCertificateData.StampImage + "&w=100&h=100&color=white";

        chkShowSign.Checked = mobjCertificateData.ShowSignature;
        chkShowStamp.Checked = mobjCertificateData.ShowStamp;
    }

    /// <summary>
    /// this function will read all controls
    /// </summary>
    private void readAllControls()
    {
        if (mobjCertificateData == null)
            mobjCertificateData = new CertificateData();

        mobjCertificateData.ManagerName = txtManagerName.Text;
        mobjCertificateData.ManagerPos = txtManagerPosition.Text;


        string strFileName = "";

        strFileName = readFile(flIamLearning);



        if (!isImagesOk)
            return;


        if (strFileName != "")
            mobjCertificateData.IamLearningImage = strFileName;



        strFileName = readFile(flSecLogo);

        if (!isImagesOk)
            return;

        if (strFileName != "")
            mobjCertificateData.SECImage = strFileName;


        strFileName = readFile(flStamp);

        if (!isImagesOk)
            return;

        if (strFileName != "")
            mobjCertificateData.StampImage = strFileName;


        strFileName = readFile(flSign);

        if (!isImagesOk)
            return;
        if (strFileName != "")
            mobjCertificateData.SignImage = strFileName;


        mobjCertificateData.ShowSignature = chkShowSign.Checked;
        mobjCertificateData.ShowStamp = chkShowStamp.Checked;



    }


    /// <summary>
    /// this function will get the file from file dialog and check if it's an image or not
    /// </summary>
    /// <param name="flSign">the file dialog</param>
    /// <returns>a string containing the uploaded file</returns>
    private string readFile(FileUpload flFile)
    {
        if (flFile.HasFile)
        {
            FileInfo fi = new FileInfo(flFile.FileName);

            string strNewFileName = System.Guid.NewGuid().ToString() + fi.Extension;

            flFile.SaveAs(Server.MapPath("./CertificateImages/" + strNewFileName));

            try
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("./CertificateImages/" + strNewFileName));
                img.Dispose();

                return strNewFileName;
            }
            catch
            {
                try { File.Delete(Server.MapPath("./CertificateImages/" + strNewFileName)); }
                catch { }
                isImagesOk = false;

                lblError.Text = "Some images is not in the right format.";

                return "";
            }


        }


        return "";
    }


    /// <summary>
    /// this function will check the security and customize page
    /// </summary>
    private void checkSecurityAndCustomizePage()
    {
        mobjPersons = GlobalVars.getCurrentPerson();

        if (!mobjPersons.lstAllRulesIDs.Contains((long)RulesEnum.Edit_certificate_details))
        {
            Response.Redirect("LoginEN.aspx");
        }


        CertificateDataDB objCertificateDataDB = new CertificateDataDB(ConnectionStrings.LMS);

        List<CertificateData> lstCertificateData = objCertificateDataDB.Select("", "");

        if (lstCertificateData.Count == 0)
        {
            Response.Redirect("LoginEN.aspx");
        }

        mobjCertificateData = lstCertificateData[0];


    }




    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            readAllControls();

            if (isImagesOk)
            {
                Save();
            }
        }
    }


    /// <summary>
    /// this function will save the certificate data to database
    /// </summary>
    private void Save()
    {
        CertificateDataDB objCertificateDataDB = new CertificateDataDB(ConnectionStrings.LMS);

        objCertificateDataDB.Update(mobjCertificateData);

        lblResult.Text = "Certificate data saved ok.";

        checkSecurityAndCustomizePage();

        BindAllControls();
    }
}